Added link_exists function, for neatness. Check for the existence of the pdev
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Tue, 15 Nov 2005 14:54:51 +0000 (15:54 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Tue, 15 Nov 2005 14:54:51 +0000 (15:54 +0100)
before issuing the diagnostic about the missing vdev.  Having a pdev but no
vdev is a strong indicator that the network-bridge script has run once already,
and so there is nothing to worry about.  Having the vdev and no corresponding
pdev is still diagnosed.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/examples/network-bridge

index 9c03a44805b43b5f0dc74dd7c6537d73030c85f7..2534b928c476bc790db19e997898fb75d7d159c0 100755 (executable)
@@ -138,6 +138,23 @@ transfer_routes () {
 }" | sh -e
 }
 
+
+##
+# link_exists interface
+#
+# Returns 0 if the interface named exists (whether up or down), 1 otherwise.
+#
+link_exists()
+{
+    if ip link show "$1" >&/dev/null
+    then
+        return 0
+    else
+        return 1
+    fi
+}
+
+
 # Usage: create_bridge bridge
 create_bridge () {
     local bridge=$1
@@ -193,8 +210,12 @@ op_start () {
        return
     fi
 
-    if ! ip link show 2>/dev/null | grep -q "^[0-9]*: ${vdev}"; then
-        echo "
+    if ! link_exists "$vdev"; then
+        if link_exists "$pdev"; then
+            # The device is already up.
+            return
+        else
+            echo "
 Link $vdev is missing.
 This may be because you have reached the limit of the number of interfaces
 that the loopback driver supports.  If the loopback driver is a module, you
@@ -202,12 +223,13 @@ may raise this limit by passing it as a parameter (nloopbacks=<N>); if the
 driver is compiled statically into the kernel, then you may set the parameter
 using loopback.nloopbacks=<N> on the domain 0 kernel command line.
 " >&2
-        exit 1
+            exit 1
+        fi
     fi
 
     create_bridge ${bridge}
 
-    if ip link show ${vdev} 2>/dev/null >/dev/null; then
+    if link_exists "$vdev"; then
        mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'`
        preiftransfer ${netdev}
        transfer_addrs ${netdev} ${vdev}
@@ -260,11 +282,11 @@ op_stop () {
     if [ "${bridge}" == "null" ]; then
        return
     fi
-    if ! ip link show ${bridge} >/dev/null 2>&1; then
+    if ! link_exists "$bridge"; then
        return
     fi
 
-    if ip link show ${pdev} 2>/dev/null >/dev/null; then
+    if link_exists "$pdev"; then
        ip link set dev ${vif0} down
        mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'`
        transfer_addrs ${netdev} ${pdev}